package com.jhj.service.bm.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.jhj.po.bm.model.BmOrder;
import com.jhj.po.bm.model.OrderResume;
import com.jhj.po.resume.model.ResumeResource;
import com.jhj.vo.bm.OrderResumeVo;
import com.jhj.service.bm.BmOrderService;
import com.jhj.service.bm.OrderResumeService;
import com.jhj.service.impl.BaseServiceImpl;
import com.jhj.service.resume.ResumeResourceService;
import com.jhj.utils.BeanUtilsExp;
import com.jhj.utils.BmUtil;
import com.jhj.utils.Constants;
import com.jhj.utils.DateUtil;
import com.jhj.utils.StringUtil;
import com.jhj.utils.TimeStampUtil;
import tk.mybatis.mapper.entity.Example;
import tk.mybatis.mapper.entity.Example.Criteria;

@Service
public class OrderResumeServiceImpl extends BaseServiceImpl<OrderResume> implements OrderResumeService {

	@Autowired
	ResumeResourceService resumeResourceService;
	
	@Autowired
	private BmOrderService orderService;
	
	@Override
	public OrderResumeVo getVo(OrderResume item) {
		OrderResumeVo vo = new OrderResumeVo();
		BeanUtilsExp.copyPropertiesIgnoreNull(item, vo);
		
		BmOrder order = orderService.selectByPrimaryKey(vo.getOrderId());
		vo.setServiceTypeName(order.getServiceTypeName());
		
		String actionTypeName = BmUtil.getOrderResumeActionType(vo.getActionType());
		vo.setActionTypeName(actionTypeName);
		
		Integer resumeId = vo.getResumeId();
		String headImg = Constants.DEFAULT_HEAD_IMG;
		ResumeResource resSearchVo = new ResumeResource();
		resSearchVo.setResumeId(resumeId);
		resSearchVo.setResourceType(Constants.RES_RESUME_HEAD_IMG);
		List<ResumeResource> resHeadImgList = resumeResourceService.selectBySearchVo(resSearchVo, "add_time desc");
		if (resHeadImgList != null && resHeadImgList.size()>0) {
			headImg = resHeadImgList.get(0).getResourceUrl();
		}
		vo.setHeadImg(headImg);
		
		
		
		
		
		
		
		String interviewTimeStr = "待定";
		if (vo.getInterviewTime() > 0) {
			interviewTimeStr = TimeStampUtil.timeStampToDateStr(Long.valueOf(vo.getInterviewTime() * 1000), DateUtil.DEFAULT_PATTERN);
		}
		vo.setInterviewTimeStr(interviewTimeStr);
		return vo;
	}
	
	@Override
	public List<OrderResume> selectBySearchVoSelf(OrderResume searchVo, String orderBy) {

		Example example = new Example(OrderResume.class);
		Criteria criteria = example.createCriteria();
		
		if (searchVo.getId() > 0) {
			criteria.andCondition("id = " + searchVo.getId());
		}
		
		if (searchVo.getOrderId() > 0) {
			criteria.andCondition("order_id = " + searchVo.getOrderId());
		}
		
		if (!StringUtil.isEmpty(searchVo.getOrderNo())) {
			criteria.andCondition("order_no = " + searchVo.getOrderNo());
		}
		
		if (searchVo.getResumeId() > 0) {
			criteria.andCondition("resume_id = " + searchVo.getResumeId());
		}
		
		if (!StringUtil.isEmpty(searchVo.getResumeNo())) {
			criteria.andCondition("resume_no = " + searchVo.getResumeNo());
		}
		
		if (!StringUtil.isEmpty(searchVo.getMobile())) {
			criteria.andCondition("mobile = " + searchVo.getMobile());
		}
		
		if (!StringUtil.isEmpty(searchVo.getName())) {
			criteria.andCondition("name = " + searchVo.getName());
		}
		
		if (!StringUtil.isEmpty(searchVo.getActionType())) {
			
			String[] actionTypeAry = StringUtil.convertStrToArray(searchVo.getActionType());
			String actionTypeIn = "";
			for (int i = 0; i < actionTypeAry.length; i++) {
				actionTypeIn+="'" + actionTypeAry[i] + "',";
			}
			actionTypeIn = actionTypeIn.substring(0, actionTypeIn.length()-1);
			criteria.andCondition("action_type in (" + actionTypeIn + ")");
		}

		// 排序
		if (!StringUtil.isEmpty(orderBy)) {
			example.setOrderByClause(orderBy);
		}
				
		List<OrderResume> list = mapper.selectByExample(example);
		return list;
	}	
}
